import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router';
import Main from '@/view/Main.vue'
import { getUserInfoApi } from '../api/auth'
const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    name: 'home',
    component: Main,
    children: [
      {
        // 宝可梦数据
        path: '/home',
        name: 'home',
        component: () => import("@/view/home/index.vue")
      },
      {
        // 宠物仓库
        path: '/petWarehouse',
        name: 'petWarehouse',
        component: () => import('@/view/warehouse/PetWarehouse.vue')
      },
      {
        // 用户
        path: '/user',
        name: 'user',
        component: () => import('@/view/user/User.vue'),
      },
      {
        // 图片墙
        path: '/uploadImg',
        name: 'uploadImg',
        component: () => import('@/view/upload/UploadImg.vue')
      },
      {
        path: '/editor',
        name: 'editor',
        component: () => import('@/components/WangEditor.vue')
      },
      {
        path: '/blog',
        name: 'blog',
        component: () => import("@/view/inline/Blog.vue")
      },
      {
        path: '/starRoaming',
        name: 'starRoaming',
        component: () => import("@/view/inline/StarRoaming.vue")
      }
    ]
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../view/auth/Login.vue')
  }
];

const router = createRouter({
  history: createWebHashHistory(),
  routes
});

// 全局路由守卫
router.beforeEach((to, from, next) => {
  // 获取是否存在token
  const token = sessionStorage.getItem('token')
  if (!token && to.name !== 'login') {
    next('login');
  } else if (token && to.name === 'login') {
    next('home');
  } else {
    next();
  }
})


export default router;